/*
 * Configuration.java
 * 
 * Created on 3.10.2007, 21:54:29
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.easyedu.dictionary.api.util;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hlavki
 */
public class Configuration {

    private static final Logger log = Logger.getLogger(Configuration.class.getName());
    protected static final String PROPERTY_RESOURCE_NAME = "eu/easyedu/dictionary/dictionary.properties";
    protected static Properties PROPERTIES;
    private static final String COLLECTIONS_VALUE_SEPARATOR = "|";
    
    public static final Set<String> QUERY_BY_EXAMPLE_INLCUDE_CLASSES = getSetOfString("jdbc.query.include.classes");

    private Configuration() {
    }

    public static String getProperty(String name) {
        return getProperty(name, null);
    }

    public static String getProperty(String name, String defaultValue) {
        if (PROPERTIES == null) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            InputStream is = cl.getResourceAsStream(PROPERTY_RESOURCE_NAME);
            if (is != null) {
                try {
                    Properties props = new Properties();
                    props.load(is);
                    PROPERTIES = props;
                } catch (IOException e) {
                    log.log(Level.SEVERE, "unable to load resource file: " + PROPERTY_RESOURCE_NAME, e);
                } finally {
                    try {
                        is.close();
                    } catch (Exception e) {
                        log.log(Level.SEVERE, "unable to close resource file: " + PROPERTY_RESOURCE_NAME, e);
                    }
                }
            }
        }
        return PROPERTIES != null ? PROPERTIES.getProperty(name, defaultValue) : defaultValue;
    }

    public static long getLongProperty(String name, long defaultValue) {
        try {
            return Long.parseLong(getProperty(name));
        } catch (NumberFormatException e) {
            log.log(Level.WARNING, e.getMessage(), e);
            return defaultValue;
        }
    }

    public static long getLongProperty(String name) {
        return getLongProperty(name, 0L);
    }

    public static int getIntProperty(String name, int defaultValue) {
        try {
            return Integer.parseInt(getProperty(name));
        } catch (NumberFormatException e) {
            log.log(Level.WARNING, e.getMessage(), e);
            return defaultValue;
        }
    }

    public static int getIntProperty(String name) {
        return getIntProperty(name, 0);
    }

    public static BigDecimal getDecimalProperty(String name, BigDecimal defaultValue) {
        BigDecimal result = defaultValue;
        try {
            String val = getProperty(name);
            if (val != null) {
                result = new BigDecimal(val);
            }
        } catch (NumberFormatException e) {
            log.log(Level.WARNING, e.getMessage(), e);
        }
        return result;
    }

    public static BigDecimal getDecimalProperty(String name) {
        return getDecimalProperty(name, null);
    }

    private static Set<String> getSetOfString(String name) {
        Set<String> result;
        String str = getProperty(name);
        if (str == null || "".equals(str)) {
            result = Collections.emptySet();
        } else {
            StringTokenizer t = new StringTokenizer(getProperty(name), COLLECTIONS_VALUE_SEPARATOR);
            result = new HashSet<String>();
            while (t.hasMoreTokens()) {
                result.add(t.nextToken().trim());
            }
        }
        return result;
    }

}